package com.jams.goodox.activity.home;


import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jams.goodox.BaseFragment;
import com.jams.goodox.BuildConfig;
import com.jams.goodox.R;
import com.jams.goodox.SePref;
import com.jams.goodox.activity.common.SimpleHtmlActivity;
import com.jams.goodox.activity.login.CourseSearchActivity;
import com.jams.goodox.activity.qa.BannerDetailActivity;
import com.jams.goodox.activity.qa.ClassLearningActivity;
import com.jams.goodox.entity.HomeCourse;
import com.jams.goodox.net.services.LoginService;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.widget.FlyBanner;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 主页
 *
 * @author jams
 */

public class HomeFragment2 extends BaseFragment {

    private static final String TAG = HomeFragment2.class.getSimpleName();
    View rootView;
    private static final String ARG_PARAM1 = "param";

    private boolean tag1 = false;
    private boolean tag2 = false;

    public MediaPlayer mediaPlayer;
    public boolean tag = false;
    public int playPosition = -100;

    @Bind(R.id.recycleView)
    RecyclerView recycleView;

    @Bind(R.id.bottom_layout)
    RelativeLayout bottom_layout;

    @Bind(R.id.close)
    ImageView close;

    @Bind(R.id.imageView)
    ImageView imageView;

    @Bind(R.id.title)
    TextView title;

    @Bind(R.id.name)
    TextView name;

    @Bind(R.id.play_menu)
    ImageView play_menu;


    @Bind(R.id.search_btn)
    TextView search_btn;

    HomeFragmentAdapter2 adapter;
    List<HomeCourse> courseList = new ArrayList<>();
    Intent intent;

    private boolean hasMorning = false;//是否有早读
    private boolean hasMorningReading = false;//是否有读书会


    Handler handler = new Handler(){

        @Override
        public void dispatchMessage(Message msg) {
            switch (msg.what){
                case 0:
                    handler.removeCallbacksAndMessages(null);
                    misLoading();
                    break;
                case 1:
                    handler.removeCallbacksAndMessages(null);
                    //重置播放菜单状态
                    play_menu.setImageResource(R.mipmap.xiafang_bofang);
                    resetStatus();
                    break;
                default:
                    handler.removeCallbacksAndMessages(null);
                    break;

            }
        }
    };


    interface OnItemClickListener {
        void OnItemClick(int position, boolean isPlay);
    }

    public static HomeFragment2 newInstance(String param) {
        HomeFragment2 fragment = new HomeFragment2();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.main_layout, container, false);
            // 初始化页面
            ButterKnife.bind(this, rootView);
        }
        ButterKnife.bind(this, rootView);
        init();
        return rootView;
    }

    private void init() {

        search_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent = new Intent(getActivity(), CourseSearchActivity.class);
                startActivity(intent);
            }
        });
        //页面滚动时停止banner滚动
        //courseList.add(new HomeCourse(3,"111"));//添加轮播图
        adapter = new HomeFragmentAdapter2(getContext(), courseList);

        recycleView.setLayoutManager(new LinearLayoutManager(getContext()));

        recycleView.setAdapter(adapter);


        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void OnItemClick(final int position, boolean isPlay) {

                Log.d(TAG, " == " + courseList.get(position).toString());
                intent = new Intent(getActivity(), ClassLearningActivity.class);
                intent.putExtra("courseId", courseList.get(position).getCourseId());
                intent.putExtra("imagePath", courseList.get(position).getImgPath());
                intent.putExtra("nickName", courseList.get(position).getNickName());
                if(courseList.get(position).getPrice() <= 0){
                    intent.putExtra("canShare",true);
                }else {
                    intent.putExtra("canShare",false);
                }
                //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
//                if (isPlay) {
//                    HomeCourse homeCourse = courseList.get(position);
//                    bottom_layout.setVisibility(View.VISIBLE);
//                    title.setText(homeCourse.getTitle());
//                    name.setText("讲师:" + homeCourse.getNickName());
//                    imageView.setImageURI(Uri.parse(homeCourse.getImgPath()));
//                    play_menu.setImageResource(R.mipmap.xiafang_zanting);
//
//                    close.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            bottom_layout.setVisibility(View.GONE);
//                            stop();
//                            resetStatus();
//                        }
//                    });
//
//                    play_menu.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            //播放菜单
//                            HomeCourse classRoomBean = courseList.get(playPosition);
////                                showLoading("正在加载...");
////                                handler.sendEmptyMessageDelayed(0,2000);
//
//                            setMedia(classRoomBean.getRadioLink());
//                            if (mediaPlayer.isPlaying()) {
//                                play_menu.setImageResource(R.mipmap.xiafang_bofang);
//                                mediaPlayer.stop();
//                                resetStatus();
//                                Log.d(TAG, "-----------暂停播放 = " + position);
//                            } else {
//                                updateOtherStatus(playPosition);
//                                mediaPlayer.start();
//                                play_menu.setImageResource(R.mipmap.xiafang_zanting);
//                                Log.d(TAG, "-----------重新播放 = " + position);
//                            }
//
//                        }
//                    });
//
//                } else {
//                    stop();
//                    bottom_layout.setVisibility(View.GONE);
//                    Log.d(TAG, " == " + courseList.get(position).toString());
//                    intent = new Intent(getActivity(), ClassLearningActivity.class);
//                    intent.putExtra("courseId", courseList.get(position).getCourseId());
//                    intent.putExtra("imagePath", courseList.get(position).getImgPath());
//                    intent.putExtra("nickName", courseList.get(position).getNickName());
//                    if(courseList.get(position).getPrice() <= 0){
//                        intent.putExtra("canShare",true);
//                    }else {
//                        intent.putExtra("canShare",false);
//                    }
//                    //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    startActivity(intent);
//                }
            }
        });

//        getHomeInfo();
    }


    private void getHomeInfo() {
        clearMap();
        params.put("requestData", "{}");

        String strEntity = gson.toJson(params);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);

        showLoading();

        createCallService(LoginService.class).homeInfo(body).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                misLoading();
                try {
                    String data = response.body().string();
                    Log.d(TAG, "home data = " + data);
                    JSONObject jsonObject = new JSONObject(data);

                    if (200 == jsonObject.getInt("code")) {
                        JSONObject object = new JSONObject(jsonObject.getString("responseData"));
                        Log.d(TAG, "responseData = " + object.toString());

                        JSONArray freeAray = object.getJSONArray("freeCourse");
                        if (null != freeAray && freeAray.length() > 0) {
                            courseList.add(new HomeCourse(0, "免费专区"));
                            //courseList.add(new HomeCourse(4, "好氧早读"));
                            for (int i = 0; i < freeAray.length(); i++) {
                                JSONObject childObj = freeAray.getJSONObject(i);
                                HomeCourse freeCourse = new HomeCourse();

                                //课程类型（0-好氧早读，1-好氧读书会）
                                if(childObj.has("courseType")){
                                    if(0 == childObj.getInt("courseType")){
                                        if(!hasMorning){
                                            courseList.add(new HomeCourse(4, "好氧早读"));
                                            hasMorning = true;
                                        }

                                        freeCourse.setMemberId(childObj.getInt("memberId"));
                                        freeCourse.setCourseId(childObj.getInt("courseId"));
                                        freeCourse.setDescr(childObj.getString("descr"));
                                        freeCourse.setFree(true);
                                        freeCourse.setType(1);
                                        freeCourse.setImgPath(childObj.getString("imgPath"));
                                        freeCourse.setNickName(childObj.getString("nickName"));
                                        freeCourse.setPrice(childObj.getInt("price"));
                                        freeCourse.setRadioLink(childObj.getString("radioLink"));
                                        freeCourse.setTitle(childObj.getString("title"));
                                        freeCourse.setTimeLength(childObj.getInt("timeLength"));
                                        freeCourse.setTranslateState(childObj.getInt("translateState"));

                                        courseList.add(freeCourse);
                                    }
                                }
                            }

                            for (int i = 0; i < freeAray.length(); i++) {
                                JSONObject childObj = freeAray.getJSONObject(i);
                                HomeCourse freeCourse = new HomeCourse();

                                //课程类型（0-好氧早读，1-好氧读书会）
                                if(childObj.has("courseType")){

                                    if(1 == childObj.getInt("courseType")){
                                        if(!hasMorningReading){
                                            courseList.add(new HomeCourse(4, "好氧读书会"));
                                            hasMorningReading = true;
                                        }
                                        freeCourse.setMemberId(childObj.getInt("memberId"));
                                        freeCourse.setCourseId(childObj.getInt("courseId"));
                                        freeCourse.setDescr(childObj.getString("descr"));
                                        freeCourse.setFree(true);
                                        freeCourse.setType(1);
                                        freeCourse.setImgPath(childObj.getString("imgPath"));
                                        freeCourse.setNickName(childObj.getString("nickName"));
                                        freeCourse.setPrice(childObj.getInt("price"));
                                        freeCourse.setRadioLink(childObj.getString("radioLink"));
                                        freeCourse.setTitle(childObj.getString("title"));
                                        freeCourse.setTimeLength(childObj.getInt("timeLength"));
                                        freeCourse.setTranslateState(childObj.getInt("translateState"));

                                        courseList.add(freeCourse);
                                    }
                                }
                            }
                        }


                        JSONArray news = object.getJSONArray("news");
                        if (null != news && news.length() > 0) {
                            courseList.add(new HomeCourse(4, "好氧头条"));
                            for (int i = 0; i < news.length(); i++) {
                                JSONObject object1 = news.getJSONObject(i);
                                HomeCourse newsItem = new HomeCourse();
                                newsItem.setTitle(object1.getString("title"));
                                newsItem.setCourseId(object1.getInt("id"));
                                newsItem.setType(5);
                                courseList.add(newsItem);
                            }
                        }

                        JSONArray greatArray = object.getJSONArray("greatCourse");
                        if (null != greatArray && greatArray.length() > 0) {
                            courseList.add(new HomeCourse(0, "精品课程"));
                            for (int i = 0; i < greatArray.length(); i++) {
                                JSONObject childObj2 = greatArray.getJSONObject(i);
                                HomeCourse greatCourse = new HomeCourse();
                                greatCourse.setMemberId(childObj2.getInt("memberId"));
                                greatCourse.setCourseId(childObj2.getInt("courseId"));
                                greatCourse.setDescr(childObj2.getString("descr"));
                                greatCourse.setFree(false);
                                greatCourse.setType(2);
                                greatCourse.setImgPath(childObj2.getString("imgPath"));
                                greatCourse.setNickName(childObj2.getString("nickName"));
                                greatCourse.setPrice(childObj2.getInt("price"));
                                greatCourse.setRadioLink(childObj2.getString("radioLink"));
                                greatCourse.setTitle(childObj2.getString("title"));
                                greatCourse.setTimeLength(childObj2.getInt("timeLength"));
                                greatCourse.setTranslateState(childObj2.getInt("translateState"));

                                courseList.add(greatCourse);
                            }
                        }

                        adapter.notifyDataSetChanged();

                    }
                } catch (JSONException e1) {
                    //e1.printStackTrace();
                } catch (Exception e) {
                    //ToastUtil.show("Exception:" + e.getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                misLoading();
                //ToastUtil.show("failure:" + t.getMessage());
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    @Override
    public void onResume() {
        super.onResume();
        courseList.clear();
        hasMorning = false;
        hasMorningReading = false;
        courseList.add(new HomeCourse(3, "111"));//添加轮播图
        getHomeInfo();
    }


    @Override
    public void onPause() {
        super.onPause();
    }


    @Override
    public void onStop() {
        super.onStop();
        stop();
    }

    private void updateOtherStatus(int pos) {
        for (int i = 0; i < courseList.size(); i++) {
            if (pos == i) {
                courseList.get(i).setPlaying(true);
            } else {
                courseList.get(i).setPlaying(false);
            }
        }

        Log.d(TAG, "downloadBeenList = " + courseList.toString());

        adapter.notifyDataSetChanged();
    }


    private void resetStatus() {
        for (HomeCourse homeCourse : courseList) {
            homeCourse.setPlaying(false);
        }

        adapter.notifyDataSetChanged();
    }


    private void setMedia(final String radioLink/*, final ImageView play_btn*/) {
        if (null == mediaPlayer) {
            mediaPlayer = new MediaPlayer();
            //mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            try {
                mediaPlayer.setDataSource(radioLink);
                mediaPlayer.prepare();
                mediaPlayer.setLooping(false);
//                play_btn.setImageResource(R.mipmap.wodexiazai_zanting);
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else {
            try {
                //mediaPlayer.reset();
                if (mediaPlayer.isPlaying()) {
//                    play_btn.setImageResource(R.mipmap.wodexiazai_bofang);
                    mediaPlayer.stop();
                } else {
//                    play_btn.setImageResource(R.mipmap.wodexiazai_zanting);
                    mediaPlayer.reset();
                    mediaPlayer.setDataSource(radioLink);
                    mediaPlayer.prepare();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
//                handler.sendEmptyMessageDelayed(0,10);
                if (TextUtils.isEmpty(radioLink)) {
                    //play_btn.setImageResource(R.mipmap.bofang_shouye);
                    handler.sendEmptyMessageDelayed(1,50);
                    ToastUtil.show("获取音频文件失败");
                    return;
                }
            }
        });

        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                //ToastUtil.show("播放结束");
//                play_btn.setImageResource(R.mipmap.bofang_shouye);
//
//                //重置播放菜单状态
//                play_menu.setImageResource(R.mipmap.bofang_shouye);
                handler.sendEmptyMessageDelayed(1,50);
                Log.d(TAG, "播放结束");
            }
        });

    }

    public void playOrPause() {
        if (mediaPlayer.isPlaying()) {
            play_menu.setImageResource(R.mipmap.xiafang_bofang);
            mediaPlayer.stop();
        } else {
            mediaPlayer.start();
            play_menu.setImageResource(R.mipmap.xiafang_zanting);
        }
    }

    public void stop() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
        }
    }

    public void release() {
        if (mediaPlayer != null) {
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }


    protected class HomeFragmentAdapter2 extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private final String TAG = HomeFragmentAdapter2.class.getSimpleName();
        private Context context;

        private List<HomeCourse> list;

        private OnItemClickListener onItemClickListener;

        public void setOnItemClickListener(OnItemClickListener listener) {
            this.onItemClickListener = listener;
        }

        public HomeFragmentAdapter2(Context context, List<HomeCourse> list) {
            this.context = context;
            this.list = list;
        }


        @Override
        public int getItemViewType(int position) {
            Log.d("TTTT", "type = " + list.get(position).getType());
            return list.get(position).getType();

        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            if (viewType == 0) {
                View view = LayoutInflater.from(context).inflate(R.layout.homefragment_text, parent, false);
                TextHolder viewHolder = new TextHolder(view);
                return viewHolder;
            }


            //免费专栏 早读  -- 标题
            if (viewType == 4) {
                View view = LayoutInflater.from(context).inflate(R.layout.homefragment_subtext, parent, false);
                SubTextHolder viewHolder = new SubTextHolder(view);
                return viewHolder;
            }


            //免费专栏早读 下内容
            if (viewType == 1) {
                View v = LayoutInflater.from(context).inflate(R.layout.homefragment_course, parent, false);
                CourseViewHolder th = new CourseViewHolder(v);
                return th;
            }

            if (viewType == 2) {
                View v = LayoutInflater.from(context).inflate(R.layout.homefragment_paycourse, parent, false);
                PayCourseViewHolder th = new PayCourseViewHolder(v);
                return th;
            }

            //header 轮播图
            if (viewType == 3) {
                View v = LayoutInflater.from(context).inflate(R.layout.homefragment_banner, parent, false);

                BannerHolder bannerHolder = new BannerHolder(v);
                return bannerHolder;
            }


            if (viewType == 5) {
                View v = LayoutInflater.from(context).inflate(R.layout.homefragment_toutiao, parent, false);
                Toutiao th = new Toutiao(v);
                return th;
            }

            return null;

        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            final HomeCourse classRoomBean = list.get(position);

            if (holder instanceof BannerHolder) {
                final List images = new ArrayList();
                final List<String> titles = new ArrayList();
                titles.add("http://www.newairedu.cn/admin/page/share/introduce");
                titles.add("http://www.newairedu.cn/admin/page/share/teacher");

                images.add(R.mipmap.banner01);
                images.add(R.mipmap.banner02);
                //设置图片加载器
                ((BannerHolder) holder).banner.setOnItemClickListener(new FlyBanner.OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        stop();
                        bottom_layout.setVisibility(View.GONE);
//                        Intent intent = new Intent(context, BannerDetailActivity.class);
                        Intent intent = new Intent(context, SimpleHtmlActivity.class);
//                        intent.putExtra("position", position);
                        intent.putExtra("url",titles.get(position));
                        context.startActivity(intent);
                    }
                });

                //设置图片集合
                ((BannerHolder) holder).banner.setImages(images);
            }

            if (holder instanceof TextHolder) {

                ((TextHolder) holder).title.setText(classRoomBean.getTitle());
            }


            if (holder instanceof SubTextHolder) {

                ((SubTextHolder) holder).title.setText(classRoomBean.getTitle());
            }

            if(holder instanceof Toutiao){
                ((Toutiao) holder).title.setText(classRoomBean.getTitle());
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        HomeCourse homeCourse = courseList.get(position);
                        Intent intent = new Intent(getContext(), SimpleHtmlActivity.class);
                        intent.putExtra("url", BuildConfig.BASE_URL
                                + "page/share/news?id="
                                +  homeCourse.getCourseId());
                        startActivity(intent);
                    }
                });
            }

            if (holder instanceof CourseViewHolder) {
                Log.d(TAG, "classRoomBean = " + classRoomBean.getType());
                ((CourseViewHolder) holder).title.setText(classRoomBean.getTitle());
//            ((CourseViewHolder) holder).nickName.setText(classRoomBean.getNickName());
//            ((CourseViewHolder) holder).timeLength.setText(TimeUtils.getTime(classRoomBean.getTimeLength()));

                final int pos = position;
//                ((CourseViewHolder) holder).title.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        Log.d(TAG, "-----点击课程-------");
//                        onItemClickListener.OnItemClick(pos, false);
//                    }
//                });

                final ImageView play_btn = ((CourseViewHolder) holder).play_btn;
                if (classRoomBean.isPlaying()) {
                    Log.d(TAG, "-----------播放 = " + position);
                    play_btn.setImageResource(R.mipmap.zanting_shouye);
                } else {
                    Log.d(TAG, "-----------暂停 = " + position);
                    play_btn.setImageResource(R.mipmap.bofang_shouye);
                }
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClickListener.OnItemClick(pos, true);
                       /* if (playPosition == -100) {//第一次打开页面 播放
                            setMedia(classRoomBean.getRadioLink()*//*, play_btn*//*);
                            playOrPause();
                            play_btn.setImageResource(R.mipmap.zanting_shouye);
                            playPosition = position;
                            return;
                        }

                        if (playPosition == position) {
                            setMedia(classRoomBean.getRadioLink()*//*, play_btn*//*);
                            if (mediaPlayer.isPlaying()) {
                                play_btn.setImageResource(R.mipmap.bofang_shouye);
                                mediaPlayer.stop();
                                Log.d(TAG, "-----------暂停播放 = " + position);
                            } else {
                                mediaPlayer.start();
                                play_btn.setImageResource(R.mipmap.zanting_shouye);
                                Log.d(TAG, "-----------重新播放 = " + position);
                            }
                            return;
                        }

                        if (playPosition != position) {
                            playPosition = position;
                            if (mediaPlayer.isPlaying()) {
                                //如果之前的在播放
                                mediaPlayer.stop();
                                setMedia(classRoomBean.getRadioLink()*//*, play_btn*//*);
                                updateOtherStatus(position);
                                play_btn.setImageResource(R.mipmap.zanting_shouye);
                                mediaPlayer.start();
                                Log.d(TAG, "-----------上一个正在播放，现在切换播放 = " + position);
                            } else {
                                //如果之前的播放已经停止，不需要额外处理
                                setMedia(classRoomBean.getRadioLink()*//*, play_btn*//*);
                                play_btn.setImageResource(R.mipmap.zanting_shouye);
                                mediaPlayer.start();
                                Log.d(TAG, "-----------上一个已经暂停，现在播放 = " + position);
                            }
                            //把之前的播放状态都设置为
                        }*/
                    }
                });
            }

            if (holder instanceof HomeFragmentAdapter2.PayCourseViewHolder) {
                ((PayCourseViewHolder) holder).title.setText(classRoomBean.getTitle());
                ((PayCourseViewHolder) holder).nickName.setText("讲师:"+classRoomBean.getNickName());
                ((PayCourseViewHolder) holder).price.setText(classRoomBean.getPrice() + "氧币");
                ((PayCourseViewHolder) holder).desc.setText(classRoomBean.getDescr());
                ((PayCourseViewHolder) holder).imageView.setImageURI(Uri.parse(classRoomBean.getImgPath()));

//            Glide.with(context).load(classRoomBean.getImgPath())
//                    //.centerCrop()
//                    .dontAnimate()
//                    .transform(new GlideCircleTransform(context)).into(((PayCourseViewHolder) holder).imageView);

                final int pos = position;
                ((PayCourseViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.d(TAG, "-----点击课程-------");
                        onItemClickListener.OnItemClick(pos, false);
                    }
                });

                final ImageView play_btn = ((PayCourseViewHolder) holder).tryListenr;
                if (classRoomBean.isPlaying()) {
                    Log.d(TAG, "-----------播放 = " + position);
                    play_btn.setImageResource(R.mipmap.zanting_shouye);
                } else {
                    Log.d(TAG, "-----------暂停 = " + position);
                    play_btn.setImageResource(R.mipmap.shiting);
                }
//                play_btn.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        //showLoading("正在加载...");
//                        //handler.sendEmptyMessageDelayed(0,1000);
//                        if (!SePref.isLogin()) {
//                            ToastUtil.show("请登录后再试！");
//                            return;
//                        }
//                        onItemClickListener.OnItemClick(pos, true);
//                        if (playPosition == -100) {//第一次打开页面 播放
//                            setMedia(classRoomBean.getRadioLink()/*, play_btn*/);
//                            playOrPause();
//                            play_btn.setImageResource(R.mipmap.zanting_shouye);
//                            playPosition = position;
//                            return;
//                        }
//
//                        if (playPosition == position) {
//                            setMedia(classRoomBean.getRadioLink()/*, play_btn*/);
//                            if (mediaPlayer.isPlaying()) {
//                                play_btn.setImageResource(R.mipmap.shiting);
//                                mediaPlayer.stop();
//                                Log.d(TAG, "-----------暂停播放 = " + position);
//                            } else {
//                                mediaPlayer.start();
//                                play_btn.setImageResource(R.mipmap.zanting_shouye);
//                                Log.d(TAG, "-----------重新播放 = " + position);
//                            }
//                            return;
//                        }
//
//                        if (/*playPosition!= -100 &&*/ playPosition != position) {
//                            playPosition = position;
//                            if (mediaPlayer.isPlaying()) {
//                                //如果之前的在播放
//                                mediaPlayer.stop();
//                                setMedia(classRoomBean.getRadioLink()/*, play_btn*/);
//                                updateOtherStatus(position);
//                                play_btn.setImageResource(R.mipmap.zanting_shouye);
//                                mediaPlayer.start();
//                                Log.d(TAG, "-----------上一个正在播放，现在切换播放 = " + position);
//                            } else {
//                                //如果之前的播放已经停止，不需要额外处理
//                                setMedia(classRoomBean.getRadioLink()/*, play_btn*/);
//                                play_btn.setImageResource(R.mipmap.zanting_shouye);
//                                mediaPlayer.start();
//                                Log.d(TAG, "-----------上一个已经暂停，现在播放 = " + position);
//                            }
//                            //把之前的播放状态都设置为
//                        }
//
//                    }
//                });
            }

        }

        @Override
        public int getItemCount() {
            return list.isEmpty() ? 0 : list.size();
        }


        class BannerHolder extends RecyclerView.ViewHolder {
            public FlyBanner banner;

            public BannerHolder(View itemView) {
                super(itemView);
                banner = (FlyBanner) itemView.findViewById(R.id.banner);
            }
        }

        //显示标题
        class TextHolder extends RecyclerView.ViewHolder {
            public TextView title;

            public TextHolder(View itemView) {
                super(itemView);
                title = (TextView) itemView.findViewById(R.id.title);
            }
        }

        //显示子标题
        class SubTextHolder extends RecyclerView.ViewHolder {
            public TextView title;

            public SubTextHolder(View itemView) {
                super(itemView);
                title = (TextView) itemView.findViewById(R.id.title);
            }
        }


        //头条
        class Toutiao extends RecyclerView.ViewHolder {
            public TextView title;

            public Toutiao(View itemView) {
                super(itemView);
                title = (TextView) itemView.findViewById(R.id.title);
            }
        }

        //课程内容
        class CourseViewHolder extends RecyclerView.ViewHolder {
            public TextView title;
            //          public TextView nickName;
//          public TextView timeLength;
            public ImageView play_btn;


            public CourseViewHolder(View itemView) {
                super(itemView);
                title = (TextView) itemView.findViewById(R.id.title);
                play_btn = (ImageView) itemView.findViewById(R.id.play_btn);
//            timeLength = (TextView)itemView.findViewById(R.id.timeLength);
//            nickName = (TextView)itemView.findViewById(R.id.nickName);
            }
        }


        //课程内容
        class PayCourseViewHolder extends RecyclerView.ViewHolder {
            public TextView title;
            public TextView nickName;
            public TextView price;
            public TextView desc;
            public SimpleDraweeView imageView;
            public ImageView tryListenr;


            public PayCourseViewHolder(View itemView) {
                super(itemView);
                title = (TextView) itemView.findViewById(R.id.title);
                desc = (TextView) itemView.findViewById(R.id.desc);
                nickName = (TextView) itemView.findViewById(R.id.nickName);
                price = (TextView) itemView.findViewById(R.id.price);
                imageView = (SimpleDraweeView) itemView.findViewById(R.id.imageView);
                tryListenr = (ImageView) itemView.findViewById(R.id.tryListenr);
            }
        }
    }
}
